<?php

namespace AI;
use Util\HttpUtil;

class BaiduAi extends IAI
{
    private static string $ak = "Kfek6uWs86ouAg68xaoXYOy3";
    private static string $sk = "G0GvjKMQBH4LGKAeqNYcwvG8IZcTGBy2";


    public function getToken(): bool|string
    {
        //使用 API Key，Secret Key 获取access_token，替换下列示例中的应用API Key、应用Secret Key
       $response =  HttpUtil::postJson("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="
            . self::$ak . "&client_secret=" . self::$sk);
        $response = json_decode($response, true);
        return $response["access_token"];
    }

    public function getUrl(): string
    {
        return "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token="
            . $this->getToken();
    }

    public function getData(array $message, string $userID): bool|string
    {
        return json_encode([
            "messages" => self::getMessagesByUid($userID, $message["content"])
        ]);
    }

    function getReplyMsg(string $reply): false|string
    {
        $response = json_decode($reply, true);
        if (!isset($response["result"])) {
            return false;
        }

        return $response["result"];
    }
}